
let GameControl =require("GameControl");
let MachineGunControl =require("MachineGunControl");

cc.Class({
    extends: cc.Component,

    properties: {

        test:cc.Node,
        canvas:cc.Node,

        rateValue: {
            // ATTRIBUTES:
            default: 1,        // The default value will be used only when the component attaching

        },
        bar: {
            get () {
                return this._bar;
            },
            set (value) {
                this._bar = value;
            }
        },

        rootCanvas:{
            default:null,
            type:cc.Node,
        },
        machineGun:{
            default:null,
            type:MachineGunControl,
        },


        speed:250,
        _isLeft:false,
        _isRight:false,


        truePos:cc.Vec2.ZERO,
        nowPos:cc.Vec2.ZERO,

        isTouch:false,

    },
    //var: _isLeft,

    // LIFE-CYCLE CALLBACKS:

    //var: truePos,
    //var: nowPos,
    onLoad () {
        //cc.log("Onload 测试");

        if(cc.sys.isBrowser){
            cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,this.onKeyDown,this);
            cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,this.onKeyUp,this);

        }




       this.truePos =cc.Vec2.ZERO;

        ///一直出错的原因是当触碰在一个点不动时，下面的事件就不是每帧触发的
        //cc.log(self.canvas.convertToWorldSpace(self.node.position));

        },

    start () {
        //cc.error(cc.sys.isMobile);
        //cc.error(cc.sys.isBrowser);

        this.node.position =cc.p(0,(-this.canvas.height/2)+10);


        let rect =cc.view.getViewPortRect();
        //console.log(rect.width);
        //console.log(rect.height);

        //console.log("misji"+GameControl.instance.testvalue);
        if(GameControl.instance===null)
        {
            console.log("这里也是空");

        }

        //cc.log("尺寸"+this.node.width);

    },
    onDestroy(){
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN,this.onKeyDown,this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP,this.onKeyUp,this);

    },

    StartGame(){
        this.truePos =cc.Vec2.ZERO;
        this.nowPos =cc.Vec2.ZERO;

        if(cc.sys.isMobile){
            var self =this;
            self.canvas.on(cc.Node.EventType.TOUCH_START,function (event) {
                self.isTouch =true;
                //self.truePos.x =self.node.x+(self.canvas.width/2);
                let touches =event.getTouches();
                self.nowPos =touches[0].getLocation();

                if(self.nowPos.x>self.truePos.x){
                    cc.log("1a"+self.nowPos.x);
                    cc.log("1a"+self.truePos.x);
                    self.StartRight();
                    self.StopLeft();
                }else if(self.nowPos.x<self.truePos.x)
                {      cc.log('1b');
                    self.StartLeft();
                    self.StopRight();
                }else{      cc.log('1c');
                    self.StopLeft();
                    self.StopRight();

                }


            },self.node);
            self.canvas.on(cc.Node.EventType.TOUCH_MOVE,function (event) {
                //self.truePos.x =self.node.x+(self.canvas.width/2);
                let touches =event.getTouches();

                self.nowPos =touches[0].getLocation();

                if(self.nowPos.x>self.truePos.x){
                    cc.log('a');
                    self.StartRight();
                    self.StopLeft();
                }else if(self.nowPos.x<self.truePos.x)
                {
                    cc.log('b');
                    self.StartLeft();
                    self.StopRight();
                }else{
                    self.StopLeft();
                    self.StopRight();
                    cc.log('c');
                }
            },self.node);

            // self.canvas.on(cc.Node.EventType.MOUSE_MOVE,function (event) {
            //     //self.truePos.x =self.node.x+(self.canvas.width/2);
            //
            //     //let touches =event.getTouches();
            //
            //     //self.nowPos =touches[0].getLocation();
            //
            //     self.nowPos =event.getLocation();
            //
            //     if(self.nowPos.x>self.truePos.x){
            //         cc.log('a');
            //         self.StartRight();
            //         self.StopLeft();
            //     }else if(self.nowPos.x<self.truePos.x)
            //     {
            //         cc.log('b');
            //         self.StartLeft();
            //         self.StopRight();
            //     }else{
            //         self.StopLeft();
            //         self.StopRight();
            //         cc.log('c');
            //     }
            // },self.node);

            self.canvas.on(cc.Node.EventType.TOUCH_END,function (event) {

                self.StopLeft();
                self.StopRight();
            },self.node);
        }



    },

    // onTouchStart(event){
    //
    //     let touches =event.getTouches();
    //     if(touches[0].getLocation().x>540)
    //     {
    //         this.StartRight();
    //         this.StopLeft();
    //     }else{
    //         this.StartLeft();
    //         this.StopRight();
    //     }
    //     //cc.log(touches[0].getLocation());
    //
    // },
    // onTouchMove(event){
    //     let touches =event.getTouches();
    //     //cc.log(touches[0].getLocation());
    //     if(touches[0].getLocation().x>540)
    //     {
    //         this.StartRight();
    //         this.StopLeft();
    //     }else{
    //         this.StartLeft();
    //         this.StopRight();
    //     }
    //
    // },
    // onTouchEnd(event){
    //     let touches =event.getTouches();
    //     //cc.log(touches[0].getLocation());
    //
    //     this.StopLeft();
    //     this.StopRight();
    // },

    onKeyDown:function(event){

        switch(event.keyCode){
            case cc.KEY.a:
            
            //console.log("an xia a");
            this.StartLeft();
            this.StopRight();
            break;
            case cc.KEY.d:
            this.StartRight();
            this.StopLeft();
            break;
            case cc.KEY.left:
                this.StartLeft();
                this.StopRight();
                break;
            case cc.KEY.right:
                this.StartRight();
                this.StopLeft();
                break;
        }
    },

    onKeyUp:function(event){

        switch(event.keyCode){
            case cc.KEY.a:
            this.StopLeft();
    
            //console.log(" song kai a");
            break;
            case cc.KEY.d:
            this.StopRight();
    
            break;
            case cc.KEY.left:
                this.StopLeft();
                break;
            case cc.KEY.right:
                this.StopRight();
                break;


            // //测试增加长度
            // case cc.KEY.b:
            //
            //     this.AddLength(500,3);
            //     break;
        }
    },

    StartRight:function()
    {
        this._isRight =true;
    },
    StopRight:function(){
        this._isRight =false;
    },
    StartLeft:function(){
        this._isLeft =true;
    },
  
    StopLeft:function(){
        this._isLeft =false;
    },
     update (dt) {
         if(this.nowPos!==cc.Vec2.ZERO&&this.truePos!==cc.Vec2.ZERO&&GameControl.instance.isPlaying===true&&this.isTouch)
         //if(this.nowPos!==cc.Vec2.ZERO&&this.truePos!==cc.Vec2.ZERO)
         {
             this.truePos.x =this.node.x+(this.canvas.width/2);

             if(this.nowPos.x>this.truePos.x+5){
                 cc.log('a');
                 this.StartRight();
                 this.StopLeft();
             }else if(this.nowPos.x<this.truePos.x-5)
             {
                 cc.log('b');
                 this.StartLeft();
                 this.StopRight();
             }else{
                 this.StopLeft();
                 this.StopRight();
                 cc.log('c');
             }
         }

      
        if(this._isLeft)
        {
           
            //if((this.node.x-this.speed*dt)>-(cc.view.getViewPortRect().width/2)+this.node.width/2)
            if((this.node.x-this.speed*dt)>-(this.rootCanvas.width/2)+this.node.width/2)
            {
                this.node.x-=this.speed*dt;
                //console.log(this.node.x);
            }
           
        }
        if(this._isRight){

            //if(this.node.x+this.speed*dt<cc.view.getViewPortRect().width/2-this.node.width/2)
            if(this.node.x+this.speed*dt<this.rootCanvas.width/2-this.node.width/2)
            {
                this.node.x+=this.speed*dt;

            }
            
        }




         // var rect =cc.view.getViewPortRect();
        // console.log(rect.width);
        // console.log(rect.height);

        // console.log("dangqian"+this.node.x);


     },

     BoolAnimate()
     {
         let actionIn =cc.scaleTo(0.05,0.9,0.9);
         let actionOut =cc.scaleTo(0.1,1.1,1.1);
         let actionReturn =cc.scaleTo(0.05,1,1);
         let action = cc.sequence(actionIn,actionOut,actionReturn);
         this.node.runAction(action);

     },


     //碰撞回调
     onCollisionEnter:function(other,self)
     {

         //console.log("On Collision enter");
         


     },


    onPostSolve: function (contact, selfCollider, otherCollider) {


        if(otherCollider.node.name==="Ball"){

            this.BoolAnimate();

            //cc.log("球"+otherCollider.node.x);
            //cc.log("bar"+selfCollider.node.x);
            let barX =selfCollider.node.x;
            let ballX =otherCollider.node.x;

            let temp =ballX-barX;
            if(temp>0)//球在右
            {
                let rate= temp*this.rateValue;

                let velocity = otherCollider.getComponent(cc.RigidBody).linearVelocity;
                velocity.x = velocity.x+rate;
                otherCollider.getComponent(cc.RigidBody).linearVelocity =velocity;


            }else{//球在左
                let rate= temp*this.rateValue;

                let velocity = otherCollider.getComponent(cc.RigidBody).linearVelocity;
                velocity.x = velocity.x+rate;
                otherCollider.getComponent(cc.RigidBody).linearVelocity =velocity;

            }

            // cc.log("碰撞盒子tag"+selfCollider.tag);
            // if(selfCollider.tag===0){//左边
            //     let velocity = otherCollider.getComponent(cc.RigidBody).linearVelocity;
            //     velocity.x = velocity.x-100;
            //     otherCollider.getComponent(cc.RigidBody).linearVelocity =velocity;
            // }
            // else{
            //     let velocity = otherCollider.getComponent(cc.RigidBody).linearVelocity;
            //     velocity.x = velocity.x+100;
            //     otherCollider.getComponent(cc.RigidBody).linearVelocity =velocity;
            // }




        }

    },


    //加速的速度和时间
    AddSpeed(speed,time){

        this.speed +=speed;


        this.scheduleOnce(function(){
            this.speed-=speed;

        },time);
    },
    DelSpeed(speed,time){

        this.speed -=speed;

        this.scheduleOnce(function(){
            this.speed+=speed;

        },time);
    },

    //增加的长度和时间
    AddLength(length,time){

        let currentWidth= this.node.width+length;
        this.node.width =currentWidth;
        let boxCollder= this.node.getComponent(cc.PhysicsBoxCollider);
        boxCollder.size.width=currentWidth;
        boxCollder.apply();

        this.scheduleOnce(function(){
            boxCollder.size.width =currentWidth-length;
            boxCollder.apply();
            this.node.width=currentWidth-length;
        },time);
    },

    DelLength(length,time){
        let currentWidth= this.node.width-length;
        this.node.width =currentWidth;
        let boxCollder= this.node.getComponent(cc.PhysicsBoxCollider);
        boxCollder.size.width=currentWidth;
        boxCollder.apply();

        this.scheduleOnce(function(){
            boxCollder.size.width =currentWidth+length;
            boxCollder.apply();
            this.node.width=currentWidth+length;
        },time);

    },
    //添加机关枪
    AddMachineGun(time){

        this.machineGun.node.active =true;
        this.machineGun.Init(this.node.width,time);

    },

});
